home *** CD-ROM | disk | FTP | other *** search
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 3
- #HS,1,4,80,25,11,1
- #C4,R5
-
- ~W~IInitializing The BGI~Y~I
-
- Setting up the BGI is basically accomplished by using the BGI ~W~Iinitgraph()~Y~I
- function. Custom initializations can be done by also using ~W~Idetectgraph()~Y~I,
- ~W~Iregisterbgidriver()~Y~I, and ~W~Iinstalluserdriver()~Y~I.
-
-
- #WN
- ~W~Iinitgraph()~Y~I
-
- This function sets up the BGI and loads a graphics driver into memory. A
- graphics driver must be loaded before the BGI can be used. There are
- actually two ways provided by the BGI to link graphics drivers to your
- program. The first is to let ~W~Iinitgraph() ~Y~Iload the appropriate driver
- whenever the program runs. If we choose this option, then the correct
- driver must be available for the program to load. That usually means
- distributing the BGI drivers on our program diskette and copying them onto
- the user's hard disk.
-
- #WN
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 4
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- This approach saves memory because only the appropriate driver is actually
- loaded into memory. However, it can be the cause of some potential problems
- if the drivers are not readily available. The user can easily delete the
- driver, then call your telephone support line (if you have one) in great
- frustration because your program won't run.
-
- #WN
- The alternative is to convert the driver to an object file, link it to
- your program, and use ~W~Iregisterbgidriver()~Y~I, which is discussed later, to
- access it.
-
- #WN
- The ~W~Iinitgraph() ~Y~Ifunction takes three parameters. The first is a pointer to
- an |int| that indicates that driver to be used. If the first parameter is
- set to ~W~IDETECT~Y~I, then ~W~Iinitgraph()~Y~I will determine what video adaptor is
- currently installed in the system, load the driver, and select the highest
- video mode that the adaptor supports. ~W~IDETECT~Y~I is one of a group of constants
- is provided in ~G~IGRAPHICS.H~Y~I that can be used to set the driver.
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 5
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- If the first parameter is anything other than ~W~IDETECT~Y~I, ~W~Iinitgraph()
- ~Y~Iwill attempt to load the |appropriate driver|. If it can't, an error message
- will be written to the screen. If DETECT is used, initgraph() will store in
- the first parameter the number corresponding to the driver that it loaded.
- That number can then be tested to see which driver the program is using.
-
- #WN
- #C4,R12
- The second parameter to ~W~Iinitgraph()~Y~I is a pointer to an int that tells the
- BGI what |video mode| to use. If ~W~IDETECT ~Y~Iwas used for the first parameter,
- then the mode that the BGI selected will be stored in this parameter. If
- anything other than ~W~IDETECT ~Y~Iwas used for the first parameter, then the
- second parameter must be set to a value that corresponds to the video mode
- that the adaptor ~W~Iinitgraph()~Y~I selected. The available modes are listed as
- named constants in ~G~IGRAPHICS.H~Y~I. They are also listed in the Turbo C library
- reference under the section on ~W~Iinitgraph()~Y~I.
-
- #WN
- #C4,R21
- The last parameter to ~W~Iinitgraph()~Y~I is the |path| of the BGI driver. If a null
- string is passed, then ~W~Iinitgraph()~Y~I will search for the driver in the
- current directory. ~SIf it is not found, the BGI will fail to initialize.~s
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 6
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- ~W~Idetectgraph()~Y~I
-
- This function is useful when you want to set the graphics system into
- something other than the BGI default. Normally the BGI puts the graphics
- adaptor into the highest mode that it supports. If for some reason we want
- to select another mode, we can use ~W~Idetectgraph()~Y~I to help us.
-
- #WN
- Figure 6.1 shows come code that might be used to set the graphics mode
- based on which adaptor is being used. In this example, the video subsystem
- is set to the lowest resolution mode if the adaptor is an EGA or VGA. It
- is set to the highest resolution on a CGA. You might ask yourself why in
- the world anyone would want to do that. Go ahead, ask yourself. I'll wait.
-
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 7
- #HS,1,4,80,25,11,1
- #C2,R5
- The answer is that ~C~Ithe lower the resolution, the more colors can be used~Y~I.
- Having lots of pixels means using lots of memory. So does having lots of
- colors. On PC adaptors, only a limited amount of memory is available, so we
- can either choose lots of pixels or lots of colors, but not both. Therefore,
- this example assumes that the game uses lots of colors in EGA and VGA, but
- that the low resolution mode of CGA is just too low resolution. In that case
- color is sacrificed for resolution.
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 8
- #HS,1,4,80,25,11,1
- #C2,R5
- ~W~IFigure 6.1~Y~I
- Using detectgraph()
-
- #include <graphics.h>
-
- main()
- { /* Start of main() */
- /* Program code goes here. */
-
- /*Now it's time to initialize the BGI. */
- set_screen();
-
- /* More program code. */
- } /* End of main() */
-
- #C28,R23
- ~C~IContinued On Next Page~Y~I
-
- #WN
- #EL,15
- #C2,R15
- ~W~I~F set_screen();~Y~I~N
- #BO,4,17,78,21,7,1,0,3,15,2
- We're ignoring most of the program. In this example, all we care about
- is the initialization of the screen. This is done by the set_screen()
- function which we will write. It's shown on the next page.
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 9
- #HS,1,4,80,25,11,1
- #C2,R5
- ~W~IFigure 6.1 (cont)~Y~I
- Using detectgraph()
-
- void set_screen(void)
- {
- int driver,mode;
- ~W~Idriver=DETECT;
- detectgraph(&driver,&mode);~Y~I
- switch(driver)
- {
- case CGA:
- mode=CGAHI;
-
- initgraph(&driver,&mode,"");
- break;
-
- #C28,R23
- ~C~IContinued On Next Page~Y~I
-
-
- #D1
- #BO,4,17,78,20,7,1,0,2,15,2
- We start by using detectgraph() to see what kind of video adaptor is
- installed.
-
- #ES,1,12,80,13
- #C2,R11
- ~Y~I driver=DETECT;
- detectgraph(&driver,&mode);
-
- #ES,1,17,80,22
- #C2,R15
- ~W~I case CGA:
- mode=CGAHI;
-
- initgraph(&driver,&mode,"");
- break;~Y~I
-
- #BO,4,9,78,13,7,1,0,3,15,2
- Here is the case that handles CGA monitors. Notice that the path (the
- third parameter to the initgraph() function) is just an empty string.
- For this to work, the BGI drivers must be in the current directory.
-
- #BO,4,7,78,13,7,1,0,5,15,2
- Since we wanted the CGA in its highest mode, we could just set the
- variable "driver" to DETECT, then call initgraph(). It would
- automatically set the screen to the highest mode possible. However,
- since we wanted to do something different for EGA and VGA monitors,
- we need to use both detectgraph() and initgraph().
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 10
- #HS,1,4,80,25,11,1
- #C2,R5
- ~W~IFigure 6.1 (cont)~Y~I
- Using detectgraph()
-
- case EGA:
- case VGA:
- /* EGALO and VGALO both equal 0. */
- mode=0;
-
- /* The assumption is that the BGI drivers */
- /* are in the current directory. */
- initgraph(&driver,&mode,"");
- break;
-
- default:
- printf("Hold it! I don't support your screen.\n");
- break;
- }
- }
-
- #D1
- #C26,R15
- ~W~I~Fdriver~Y~I~N
- #BO,4,17,78,21,7,1,0,3,15,2
- The variable "driver" received a value during the call to the
- detectgraph() function back on page 9. So we don't need to initialize
- it.
-
- #C26,R15
- ~Y~I~Ndriver
- #C15,R11
- ~W~I~Fmode~Y~I~N
- #BO,4,17,78,22,7,1,0,4,15,2
- The variable "mode" also received a value when we called detectgraph()
- on page 8. However, we want to override that value, so we store a 0
- in it. We could use the named constants EGALO, or VGALO. They are
- defined in GRAPHICS.H. They both equal 0.
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 11
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~Iregisterbgidriver()~Y~I
-
- If you want all of the drivers that your program supports to be part of
- the program itself, you can convert the bgi driver to an object file with
- ~G~IBINOBJ.EXE~Y~I. This is a tool that comes with Turbo C.
-
- #WN
- Once the driver is in object format, it can be linked into your program
- just like any other object file. Simply insert the name of the driver,
- which should now have a .OBJ extension on the file name, into your project
- file. When your program is compiled and linked, any driver listed in your
- project file will be linked in.
-
- #WN
- However, the BGI doesn't know the drivers are linked in unless you tell it.
- The function ~W~Iregisterbgidriver() ~Y~Ipasses this information to the BGI. Before
- ~W~Iinitgraph() ~Y~Iis called, ~W~Iregisterbgidriver() ~Y~Imust be called once for
- each driver that you linked into your program. A constant is passed to this
- function that tells the BGI which driver to register. The constants are
- defined in GRAPHICS.H.
-
- #WP
- %
- #EF
- #T15,1,Chapter 6 The Borland Graphics Interface Pg. 12
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- If, for example, you had linked the CGA, EGA and VGA drivers into your
- program and wanted to register them, you would make the following two calls.
- ~W~I
- registerbgidriver(CGA_driver);
- registerbgidriver(EGAVGA_driver);
- ~Y~I
-
- #WN
- The ~W~Iinitgraph() ~Y~Ifunction could then be called to start the graphics system
- with whichever of these drivers was appropriate.
-
- #WP
- #X